package com.glela.common.util;

import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

@Repository
public class Log4jConfiguration {

	Logger logger = LoggerFactory.getLogger(Log4jConfiguration.class);

//	@PostConstruct
	public void initLog4jLevel() {
		logger.debug("====================log4j level seting start====================");
		String logLevel = Config.getString("log4j_level");
		if (logLevel == null) {
			logLevel = "";
		}
		if (logLevel.length() > 0 && Level.toLevel(logLevel) != null) {
			LogManager.getRootLogger().setLevel(Level.toLevel(logLevel));
			logger.info("Reset rootLogger log4j log level:" + logLevel);
			if (LogManager.getLogger("com.glela") != null) {
				logger.info("Reset com.glela log4j log level:" + logLevel);
				LogManager.getLogger("com.glela").setLevel(Level.toLevel(logLevel));
			}
			if (LogManager.getLogger("org.apache.ibatis") != null) {
				logger.info("Reset org.apache.ibatis log4j log level:"+ logLevel);
				LogManager.getLogger("org.apache.ibatis").setLevel(Level.toLevel(logLevel));
			}
//			if (LogManager.getLogger("org.mybatis.spring") != null) {
//				logger.info("Reset java.sql log4j log level:" + logLevel);
//				LogManager.getLogger("org.mybatis.spring").setLevel(Level.toLevel(logLevel));
//			}
			if (LogManager.getLogger("java.sql") != null) {
				logger.info("Reset java.sql log4j log level:" + logLevel);
				LogManager.getLogger("java.sql").setLevel(Level.toLevel(logLevel));
			}
			if (LogManager.getLogger("java.sql.Connection") != null) {
				logger.info("Reset java.sql.Connection log4j log level:" + logLevel);
				LogManager.getLogger("java.sql.Connection").setLevel(Level.toLevel(logLevel));
			}
			if (LogManager.getLogger("java.sql.Statement") != null) {
				logger.info("Reset java.sql.Statement log4j log level:" + logLevel);
				LogManager.getLogger("java.sql.Statement").setLevel(Level.toLevel(logLevel));
			}
			if (LogManager.getLogger("java.sql.PreparedStatement") != null) {
				logger.info("Reset java.sql.PreparedStatement log4j log level:" + logLevel);
				LogManager.getLogger("java.sql.PreparedStatement").setLevel(Level.toLevel(logLevel));
			}
//			if (LogManager.getLogger("org.springframework") != null) {
//				logger.info("Reset java.sql log4j log level:" + logLevel);
//				LogManager.getLogger("org.springframework").setLevel(Level.toLevel(logLevel));
//			}
			logger.debug("====================log4j level seting end====================");
		}
	}
}
